<template>
  <div>
    <transition
      name="slide-fade" mode="out-in"
    >
      <custom-container v-if="!detailVisible">
    <custom-container-header no-spacing>
      <el-form :model="filters" ref="form" :disabled="loading"
               inline size="medium"
               @submit.native.prevent="getAll"
               class="width-100"
      >
        <el-form-item label="查询时间:">
          <el-date-picker
            v-model="filters.date"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="{disabledDate: beforeToday.disabledDate}"
            value-format="timestamp"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
        <!--<el-form-item>-->
          <!--<el-button type="primary" native-type="submit">查询</el-button>-->
        <!--</el-form-item>-->

        <el-form-item class="float-right">
          <custom-button-group v-model="time">
            <custom-button v-for="period in periods" :key="period.value" :label="period.value">
              {{period.title}}
            </custom-button>
          </custom-button-group>
        </el-form-item>
      </el-form>
    </custom-container-header>
    <custom-container-body>
      <el-table :data="tableData.list" :loading="loading" element-loading-text="加载中..." border border-radius
                :summary-method="getSummaries"
                :local-summary-method="getLocalSummaries"
                show-summary
                show-local-summary
                @sort-change="sortChange"
      >
        <el-table-column
          align="center"
          prop="date"
          label="日期">
        </el-table-column>

        <el-table-column
          align="center"
          prop="bonusUser"
          label="红利人数">
          <template slot-scope="{row}">
            <el-button type="link" size="small"
                       @click="openDetail({detailName: 'bonus-detail', props: {row, filters}})"
            >
              {{row.bonusUser}}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="bonus"
          label="红利总额">
          <template slot-scope="{row}">
            {{row.bonus | currency}}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateUser"
          label="返水人数">
          <template slot-scope="{row}">
            <el-button type="link" size="small"
                       @click="openDetail({detailName: 'rebate-detail', props: {row, filters}})"
            >
              {{row.rebateUser}}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebate"
          label="返水总额">
          <template slot-scope="{row}">
            {{row.rebate | currency}}
          </template>
        </el-table-column>

        <template slot="empty">
          <div class="el-table__empty">
            <svg-icon class="el-table__empty-icon" icon-class="grid-empty-2"></svg-icon>
            <div class="el-table-text">暂无数据</div>
          </div>
        </template>
      </el-table>

      <div class="el-pagination-container">
        <el-pagination
          bordered
          @size-change="localGridSizeChange" @current-change="localGridCurrentChange"
          :current-page="filters.page" :page-size="filters.size"
          :total="tableData.total">
        </el-pagination>
      </div>
    </custom-container-body>
  </custom-container>
      <component
        :is="detailName"
        :info="detailProps"
        v-else
      ></component>
    </transition>
  </div>
</template>

<script type="text/jsx">

  import {
    bonusRebateReportApi as getApi,
  } from '@/services/api/report';

  import {grid, pickerOption, detailContent} from '@/build';

  import BonusDetail from "./bonus-detail";
  import RebateDetail from "./rebate-detail";

  export default {
    name: "game-win-lose",

    components: {
      BonusDetail,
      RebateDetail
    },

    mixins: [grid, pickerOption, detailContent],

    data() {
      return {
        filters: {
          date: undefined,
          page: 1,
          size: 10,
        },
        time: consts.THIS_WEEK,
        periods: _.filter(consts.TIME_PERIOD, (period) => {
          return _.indexOf([consts.THIS_WEEK, consts.LAST_WEEK, consts.THIS_MONTH, consts.LAST_MONTH], period.value) !== -1;
        }),
        tableData: {
          list: [],
          total: 0,
        },
        statistic: [
          {
            index: 1,
            prop: 'tableData.sum.bonusUser',
          },
          {
            index: 2,
            prop: 'tableData.sum.bonus',
            format: (value) => {
              return <div>{_.currency(value)}</div>;
            }
          },
          {
            index: 3,
            prop: 'tableData.sum.rebateUser',
          },
          {
            index: 4,
            prop: 'tableData.sum.rebate',
            format: (value) => {
              return <div>{_.currency(value)}</div>;
            }
          },
        ]
      };
    },

    watch: {
      'filters.date'() {
        this.gridSearch();
      },
      'filters.page'() {
        this.doGet({action: 'getGrid'});
      }
    },

    mounted() {
      this.bindGet(getApi, 'tableData', {
        action: 'getGrid',
        sendingData: 'filters',
        format: data => {
          data.list = _(data.list).map((info, date) => {
            return {
              date,
              ...info
            };
          }).orderBy(['date'], ['desc']).value();
          // data.total = _.size(data.list);
          return data;
        }
      });
    }
  };

</script>

